package com.it.myd.controller.main.module;

import com.it.myd.bean.box.UserGameInfo;
import com.it.myd.bean.box.game.WuziqiRV;
import com.it.myd.bean.box.game.Room;
import com.it.myd.bean.main.ReturnValue;
import com.it.myd.bean.main.User;
import com.it.myd.service.main.module.room.RoomService;
import com.it.myd.service.main.module.wuziqi.WuZiQiService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.socket.WebSocketSession;

import java.util.List;

@Controller
@RequestMapping("/module/wuziqi")
public class WuZiQiController {

    @Autowired
    WuZiQiService wuZiQiService;
    @Autowired
    RoomService roomService;

    public ReturnValue joinMatcher(String userid, String type, String state, WebSocketSession session){
        ReturnValue returnValue = new ReturnValue();
        UserGameInfo ugi = new UserGameInfo();
        User user = new User();
        user.setUser_id(Integer.parseInt(userid));
        ugi.setUser(user);
        ugi.setType(type);
        List<UserGameInfo> ugiList = wuZiQiService.getUserGameInfo(ugi);
        if("0".equals(state)){
            if(ugiList.size() == 0){
                List<User> userList = wuZiQiService.getUserByUserid(userid);
                if(userList.size() == 0){
                    returnValue.isFail("userid未匹配到用户 userid:"+userid);
                    return returnValue;
                }
                wuZiQiService.addUserGameInfo(ugi);
            }else{
                ugi = ugiList.get(0);
                if(ugi.getState().equals("1")){
                    returnValue.isFail("请不要使用同一个账号同时进入同一个游戏");
                    return returnValue;
                }
                ugi.setState("1");
                wuZiQiService.updateUserGameInfo(ugi);
            }
            returnValue.isSuccess("上线成功!");
        }else if("1".equals(state)){
            ugi = ugiList.get(0);
            wuZiQiService.playeradd(ugi,session);
            returnValue.isSuccess("正在匹配");
        }else if("-1".equals(state)){
            if(ugiList.size()>0){
                ugi = ugiList.get(0);
                ugi.setState("-1");
                wuZiQiService.updateUserGameInfo(ugi);
                returnValue.isSuccess("取消匹配");
            }
        }else if("-2".equals(state)){
            if(ugiList.size()>0){
                ugi = ugiList.get(0);
                ugi.setState("0");
                wuZiQiService.updateUserGameInfo(ugi);
            }
        }


        return returnValue;
    }

    @RequestMapping("setQz")
    @ResponseBody
    public void setQz(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String x = request.getParameter("x");
        String y = request.getParameter("y");
        WuziqiRV wuziqiRV = new WuziqiRV();
        wuziqiRV.isSuccess("对手已下");
        wuziqiRV.setX(x);
        wuziqiRV.setY(y);
        wuziqiRV.setUserid(String.valueOf(user.getUser_id()));
        wuZiQiService.cellQz(wuziqiRV);
    }

    @RequestMapping("backHome")
    @ResponseBody
    public ReturnValue backHome(HttpServletRequest request){
        ReturnValue returnValue = new ReturnValue();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String type = request.getParameter("type");

        WuziqiRV wuziqiRV = new WuziqiRV();
        wuziqiRV.isSuccess("对手已离开");
        wuziqiRV.setUserid(String.valueOf(user.getUser_id()));
        wuZiQiService.cellQz(wuziqiRV);
        wuZiQiService.gamePlayerOut(user.getUser_id());
        returnValue.isSuccess();
        return returnValue;
    }

    @RequestMapping("wingame")
    @ResponseBody
    public void winGame(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Room room = roomService.getRoomByUserId(user.getUser_id());
        room.setState("3");
        room.setWinuser_ids(String.valueOf(user.getUser_id()));
        wuZiQiService.updateGameInfo(room);
        UserGameInfo ugi_t = new UserGameInfo();
        ugi_t.setUser(user);
        UserGameInfo ugi = wuZiQiService.getUserGameInfo(ugi_t).get(0);
        ugi.setScore(String.valueOf(Integer.valueOf(ugi.getScore())+1));
        ugi.setWincount(String.valueOf(Integer.valueOf(ugi.getWincount())+1));
        ugi.setPlaycount(String.valueOf(Integer.valueOf(ugi.getPlaycount())+1));
        wuZiQiService.updateUserGameInfo(ugi);
    }

    @RequestMapping("losegame")
    @ResponseBody
    public void loseGame(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        UserGameInfo ugi_t = new UserGameInfo();
        ugi_t.setUser(user);
        UserGameInfo ugi = wuZiQiService.getUserGameInfo(ugi_t).get(0);
        ugi.setScore(String.valueOf(Integer.valueOf(ugi.getScore())-1));
        ugi.setPlaycount(String.valueOf(Integer.valueOf(ugi.getPlaycount())+1));
        wuZiQiService.updateUserGameInfo(ugi);
    }
}
